package com.hzy.utils.pay;

import java.security.MessageDigest;
import java.util.*;

public class SignCreateUtil {
    public static void main(String[] args) {
        String key = "1234567";
        SortedMap<String , String> arrays = new TreeMap<>();
        arrays.put("appid" , "wxd930ea5d5a258f4f");
        arrays.put("mch_id" , "10000100");
        arrays.put("device_info" , "1000");
        arrays.put("body" , "test");
        arrays.put("nonce_str" , "ibuaiVcKdpRxkhJA");
        String sign = SignCreateUtil.createSign(key, arrays);
        System.out.println(sign);

/**
 *  09BED67F77E9D359A5014C4DAE84F42E  使用微信签名工具生成的签名，用作比较
 *  09BED67F77E9D359A5014C4DAE84F42E
 */

    }
    public static String  createSign(String key, SortedMap<String ,String> parameters){
        SortedMap<String, String> stringSortedMap = paraFilter(parameters);  //参数过滤
        StringBuffer sb = new StringBuffer();  //创建拼接字符串对象
        Set es = stringSortedMap.entrySet();   //遍历SortedMap
        Iterator it = es.iterator();
        while (it.hasNext()) {
            Map.Entry entry = (Map.Entry) it.next();
            String k = (String) entry.getKey();
            Object v = entry.getValue();
            if (null != v && !"".equals(v)
                    && !"sign".equals(k) && !"key".equals(k)) {
                sb.append(k + "=" + v + "&");     //开始拼接字符串
            }
        }
        sb.append("key=" + key);
        String sign = MD5Encode(sb.toString(), "UTF-8").toUpperCase();  //把拼接好的字符串MD5加密生成签名
        return sign;

    }
    private static SortedMap<String, String> paraFilter(SortedMap<String, String> sArray) {
        SortedMap<String, String> result = new TreeMap<String,String>();  //构建新的容器用作返回
        if (sArray == null || sArray.size() <= 0) {
            return result;
        }
        for (String key : sArray.keySet()) {
            String value = sArray.get(key);
            if (value == null || "".equals(value) || "sign".equalsIgnoreCase(key)
                    || "sign_type".equalsIgnoreCase(key)) {
                continue;
            }
            result.put(key, value);
        }
        return result;
    }
    private static String MD5Encode(String origin, String charsetName) {
        String resultString = null;  //构建返回字符串
        try {
            resultString = new String(origin);
            MessageDigest md = MessageDigest.getInstance("MD5");
            if (charsetName == null || "".equals(charsetName)) {
                resultString = byteArrayToHexString(md.digest(resultString
                        .getBytes()));
            } else {
                resultString = byteArrayToHexString(md.digest(resultString
                        .getBytes(charsetName)));
            }
        } catch (Exception exception) {
            exception.printStackTrace();
        }
        return resultString;
    }
    private static String byteArrayToHexString(byte b[]) {
        StringBuffer resultSb = new StringBuffer();
        for (int i = 0; i < b.length; i++) {
            resultSb.append(byteToHexString(b[i]));
        }

        return resultSb.toString();
    }
    private static String byteToHexString(byte b) {
        int n = b;
        if (n < 0) {
            n += 256;
        }
        int d1 = n / 16;
        int d2 = n % 16;
        return hexDigits[d1] + hexDigits[d2];
    }
    private static final String hexDigits[] = { "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "a", "b", "c", "d", "e", "f" };


}
